﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OneMake.Model
{
    //每日活动效果
    public class report_day_activity
    {
        [MapToField(FieldName = "店铺编号")]
        public int shopid { get; set; }

        [MapToField(FieldName = "日期")]
        public DateTime stat_datetime { get; set; } 
        //	
        [MapToField(FieldName = "店铺活动")]
        public string activity { get; set; }
        //	
        [MapToField(FieldName = "活动类别")]
        public string hd_type { get; set; }
        //	
        [MapToField(FieldName = "数量")]
        public int alipay_num2 { get; set; }
        //	
        //[MapToField(FieldName = "数量占比")]
        //public double sale_rate { get; set; }
        //	
        [MapToField(FieldName = "金额")]
        public double alipay_amount { get; set; }
        //	
        //[MapToField(FieldName = "金额占比")]
        //public double sale_total_rate { get; set; }
        //
        //[MapToField(FieldName = "备注")]
        //public string remark { get; set; }

    }

    partial class ReportHelper
    {
        public string get_shop_name(int shopId)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select name from conf_shop where shopid=@0";
            return db.SafeExecuteScalar<string>(sql, shopId);
        }

        public List<report_day_activity> get_report_hdxg(string keyword, int shopId, DateTime day)
        {
            List<report_day_activity> reports = new List<report_day_activity>();
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            db.OneTimeCommandTimeout = int.MaxValue;

            //string sql1 = "select * from lz_d_prdsale where prd_name like @0 and shopid=@1 and stat_datetime=@2";
            string shopName = get_shop_name(shopId);
            string sql1 = @"select lz_d_prdsale.shopid,lz_d_prdsale.stat_datetime,lz_d_prdsale.alipay_num2,lz_d_prdsale.alipay_amount,jhs_d_yxhd.activity from lz_d_prdsale left join jhs_d_yxhd 
                            on lz_d_prdsale.stat_datetime=jhs_d_yxhd.stat_datetime
                            where lz_d_prdsale.prd_name like @0 and lz_d_prdsale.shopid=@1 and lz_d_prdsale.stat_datetime=@2 and jhs_d_yxhd.shopname=@3 ";
            report_day_activity hdxg1 = db.FirstOrDefault<report_day_activity>(sql1, string.Format("%{0}%", keyword), shopId, day.ToShortDateString(), shopName);
            if (hdxg1 == null)
            {
                hdxg1 = new report_day_activity();
                hdxg1.shopid = shopId;
                hdxg1.stat_datetime = day;
            }
            hdxg1.hd_type = keyword;//活动类别
/*
            report_day_activity hdxg1 = new report_day_activity();
            hdxg1.stat_datetime = day.Date;//日期	
            hdxg1.activity = keyword;//店铺活动
            hdxg1.hd_type = keyword;//活动类别	
            if (prdsale1 != null)
            {
                hdxg1.alipay_num2 = prdsale1.alipay_num2; //数量	
                //hdxg1.sale_rate { get; set; }//数量占比	
                hdxg1.alipay_amount = prdsale1.alipay_amount; //金额	
                //hdxg1.sale_total_rate { get; set; }//金额占比
                hdxg1.remark = "";//备注
            }

            db.OneTimeCommandTimeout = int.MaxValue;
            string sql2 = "select * from lz_d_prdsale where prd_name like @0 or prd_name like @1 and shopid=@2 and stat_datetime=@3";
            lz_d_prdsale prdsale2 = db.FirstOrDefault<lz_d_prdsale>(sql2, "%热卖%", "%体验%", shopId, day.ToShortDateString());
            report_day_activity hdxg2 = new report_day_activity();
            hdxg2.stat_datetime = day.Date;//日期	
            hdxg2.dphd = act;//店铺活动
            hdxg2.hd_type = "品牌特卖";//活动类别
            if (prdsale2 != null)
            {
                hdxg2.sale_num = prdsale2.alipay_num2; //数量	
                //hdxg2.sale_rate { get; set; }//数量占比	
                hdxg2.sale_total = prdsale2.alipay_amount; //金额	
                //hdxg2.sale_total_rate { get; set; }//金额占比
                hdxg2.remark = "";//备注
            }

            db.OneTimeCommandTimeout = int.MaxValue;
            string sql3 = "select * from lz_d_prdsale where prd_name not like @0 and prd_name not like @1 and prd_name not like @2 and shopid=@3 and stat_datetime=@4";
            lz_d_prdsale prdsale3 = db.FirstOrDefault<lz_d_prdsale>(sql3, "%热卖%", "%体验%", "%品牌特卖%", shopId, day.ToShortDateString());
            report_day_activity hdxg3 = new report_day_activity();
            hdxg3.stat_datetime = day.Date;//日期	
            hdxg3.dphd = act;//店铺活动
            hdxg3.hd_type = "日常款";//活动类别	
            if (prdsale3 != null)
            {
                hdxg3.sale_num = prdsale3.alipay_num2; //数量	
                //hdxg3.sale_rate { get; set; }//数量占比	
                hdxg3.sale_total = prdsale3.alipay_amount; //金额	
                //hdxg3.sale_total_rate { get; set; }//金额占比
                hdxg3.remark = "";//备注
            }

            report_day_activity hdxg4 = new report_day_activity();
            hdxg4.stat_datetime = day.Date;//日期	
            hdxg4.dphd = act;//店铺活动
            hdxg4.hd_type = "总计";//活动类别	
            hdxg4.sale_num = hdxg1.sale_num + hdxg2.sale_num + hdxg3.sale_num;//数量	
            hdxg4.sale_rate = 1.0; //数量占比	
            hdxg4.sale_total = hdxg1.sale_total + hdxg2.sale_total + hdxg3.sale_total; //金额	
            hdxg4.sale_total_rate = 1.0; //金额占比
            hdxg4.remark = "";//备注

            if (hdxg4.sale_num > 0)
            {
                hdxg1.sale_rate = hdxg1.sale_num / hdxg4.sale_num; //数量占比
                hdxg2.sale_rate = hdxg2.sale_num / hdxg4.sale_num; //数量占比
                hdxg3.sale_rate = hdxg3.sale_num / hdxg4.sale_num; //数量占比
            }

            if (hdxg4.sale_total > 0)
            {
                hdxg1.sale_total_rate = hdxg1.sale_total / hdxg4.sale_total; //金额占比
                hdxg2.sale_total_rate = hdxg2.sale_total / hdxg4.sale_total; //金额占比
                hdxg3.sale_total_rate = hdxg3.sale_total / hdxg4.sale_total; //金额占比      
            }
*/
            reports.Add(hdxg1);
            //reports.Add(hdxg2);
            //reports.Add(hdxg3);
            //reports.Add(hdxg4);

            return reports;
        }
        public List<report_day_activity> get_report_hdxg(string keyword, int shopId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            List<report_day_activity> reports = new List<report_day_activity>();
            DateTime tempTime = beginTime;
            do
            {
                reports.AddRange(get_report_hdxg(keyword, shopId, tempTime));
                tempTime = tempTime.AddDays(1);
            }
            while (tempTime <= endTime);

            return reports;
        }
    }
}
